﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameEngine
{
    public static class Camera
    {
        #region Attributes

        private static Vector2 origin = Vector2.Zero;
        private static Vector2 position = Vector2.Zero;

        private static float zoom = 1.0f;
        private static float rotation = 0.0f;
        private static Rectangle roomRect;
        private static Rectangle viewPortRect;


        #endregion

        #region Properties

        public static Vector2 Origin
        {
            get { return origin; }
            set { origin = value; }
        }

        public static float Zoom
        {
            get { return zoom; }
            set { zoom = value; }
        }

        public static float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }

        public static int WidthLimit
        {
            get { return roomRect.Width; }
        }

        public static int HeightLimit
        {
            get { return roomRect.Height; }
        }

        public static Rectangle ViewPortRect
        {
            get { return viewPortRect; }
            set { viewPortRect = value; }
        }

        public static Rectangle RoomRect
        {
            get { return roomRect; }
            set { roomRect = value; }
        }

        public static void Initilize(Vector2 pos, Rectangle roomBounds, Rectangle viewPortS)
        {
            Position = pos;
            RoomRect = roomBounds;
            ViewPortRect = viewPortS;
            Origin = new Vector2(viewPortS.Width / 2, viewPortS.Height / 2);
        }

        /// <summary>
        /// Set the Position of the camera with bouderyes of the current room
        /// </summary>
        public static Vector2 Position
        {
            get { return position; }
            set
            {
                position = new Vector2(
                    MathHelper.Clamp(value.X, WidthLimit - viewPortRect.Width, Origin.X + WidthLimit - viewPortRect.Width),          //Kollar så att kameran håller sig innan för sin max och min-gräns
                    MathHelper.Clamp(value.Y, HeightLimit - viewPortRect.Height, Origin.Y + HeightLimit - viewPortRect.Height));
            }
        }

        #endregion

        #region Public Methods

        public static Matrix GetViewMatrix()
        {
            return Matrix.CreateTranslation(new Vector3(-Position, 0f))
                 * Matrix.CreateScale(new Vector3(Zoom, Zoom, 1f))   //Scales all objecs according to the X- and Y- axis 
                 * Matrix.CreateRotationZ(Rotation)                  //Rotate the camera
                 * Matrix.CreateTranslation(new Vector3(Origin, 0f));//Flyttar kameran så att dess position är centrerad i bilden, i stället för uppe i högra hörnet
        }

        /// <summary>
        /// Translates a vector from world coordinates to screen coordinates
        /// </summary>
        /// <param name="worldPos"></param>
        /// <returns></returns>
        public static Vector2 WorldToScreen(Vector2 worldPos)
        {
            return worldPos + position  - new Vector2(ViewPortRect.Width / 2, ViewPortRect.Height / 2);
        }


        #endregion
    }
}
